package com.rc.saas.mini.config;

import com.rc.saas.mini.minip.interceptor.MemberInterceptor;
import com.rc.saas.mini.minip.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * spring boot @ImportResource 拦截器不起作用 ???
 * SpringBoot2使用的Spring5，因此将WebMvcConfigurerAdapter改为WebMvcConfigurer
 * 使用WebMvcConfigurer扩展SpringMVC好处既保留了SpringBoot的自动配置，又能用到我们自己的配置
 *
 * @EnableWebMvc 如果我们需要全面接管SpringBoot中的SpringMVC配置则开启此注解，开启后，SpringMVC的自动配置将会失效。
 *
 * Created by sven on 2019/9/29
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
    //微信接入认证控制和菜单事件监听器不能拦截，否则无法通过微信验证
    //private final static String weixinauth_url = "/weChat/*/main";
    private final static String weixinauth_url = "/weChat/main";


    /**
     * 初始化拦截器,并将bean交给Spring容器实例化
     * @return
     */
    @Bean
    public MemberInterceptor memberInterceptor() {
        return new MemberInterceptor();
    }

    /**
     * 初始化TOKEN拦截器,并将bean交给Spring容器实例化
     * @return
     */
    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    /**
     * 同时在配置拦截器时候，添加拦截器不能使用new AppInterceptor()，
     * 而是应该使用@Bean注册的方法，进行方法调用添加到拦截器配置里
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // <!--本地开发不需要，最好关掉，否则跑不起来；微信公众号上线前必须打开,否则找不到openid-->
        //<mvc:interceptors>
        //	<mvc:interceptor>
        //		<mvc:mapping path="/**"/>
        //		<mvc:exclude-mapping path="/front/css/*"/>
        //		<mvc:exclude-mapping path="/front/img/*"/>
        //		<mvc:exclude-mapping path="/front/js/*"/>
        //		<mvc:exclude-mapping path="/*.txt"/>
        //		<mvc:exclude-mapping path="/weChat/*"/>
        //		<ref bean="memberFilter"/>
        //	</mvc:interceptor>
        //</mvc:interceptors>

        //TOKEN拦截器
        registry.addInterceptor(tokenInterceptor())
                //拦截项目中的哪些请求
                .addPathPatterns("/**")
                .excludePathPatterns("/appjs/**",
                        "/libjs/**",
                        "/static/**",
                        "/include/**",
                        "/templates/**",
                        "/locales/**",
                        "/captcha/**",
                        "/welcome",
                        "/home",
                        "/console",
                        "/resource/**",
                        "/error",
                        "/toShare/**",
                        "/swagger-ui.html",
                        "/**/swagger-ui/**",
                        "/**/swagger-resources/**",
                        "/**/v3/**",
                        "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/*.html", "/**/*.html", "/swagger-resources/**",
                        weixinauth_url,
                        "/user/getAuthCode",
                        "/user/loginByAuthCode",
                        "/user/loginByUnionId",
                        "/micro/**",
                        "/**/open/**"
                );


        registry.addInterceptor(memberInterceptor())
                //拦截项目中的哪些请求
                .addPathPatterns("/**");
    }
}
